/*
 * Team Theta
 * CTC Office
 * CTC Class
 * Yuxin Liu
 */
package edu.pitt.coe1186.theta.ctc;


import edu.pitt.coe1186.theta.ctc.CTC;
import static java.lang.Math.random;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.BoxBlur;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.util.Duration;

public class Login extends Application {


    Scene scene;
    @Override 
    public void start(Stage primaryStage) {
        final Stage stage = primaryStage;
        Group root = new Group();
        scene = new Scene(root, 1200, 830, Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.setTitle("CTC Login");
        Rectangle colors = (Rectangle)color(scene);
        Group circles = new Group();
        
        
        for (int i = 0; i < 30; i++) {
            Circle circle = new Circle(150, Color.web("white", 0.05));
            circle.setStrokeType(StrokeType.OUTSIDE);
            circle.setStroke(Color.web("white", 0.16));
            circle.setStrokeWidth(4);
            circles.getChildren().add(circle);
        }
        
        circles.setEffect(new BoxBlur(10, 10, 3));
        
        
        
        Group blendModeGroup = 
            new Group(new Group(new Rectangle(scene.getWidth(), scene.getHeight(),
        Color.BLACK), circles), colors);
        colors.setBlendMode(BlendMode.OVERLAY);
        root.getChildren().add(blendModeGroup);
        
        Timeline timeline = new Timeline();
        for (Node circle: circles.getChildren()) {
            timeline.getKeyFrames().addAll(
                new KeyFrame(Duration.ZERO, // set start position at 0
                    new KeyValue(circle.translateXProperty(), random() * scene.getWidth()),
                    new KeyValue(circle.translateYProperty(), random() * scene.getHeight())
                ),
                new KeyFrame(new Duration(70000), // set end position at 100s
                    new KeyValue(circle.translateXProperty(), random() * scene.getWidth()),
                    new KeyValue(circle.translateYProperty(), random() * scene.getHeight())
                )
            );
        }
        
        // 
        final HBox login_box = (HBox)text("Login:   ");
        final HBox psw = (HBox)pswText("Password:   ");
        psw.setLayoutY(psw.getLayoutY()+30);
        psw.setSpacing(0);
        root.getChildren().add(login_box);
        root.getChildren().add(psw);
        
        final Label warning = new Label("");
        warning.setTextFill(Color.rgb(255, 0, 0));
        warning.setLayoutX(scene.getWidth()*0.452);
        warning.setLayoutY(scene.getHeight()*0.6);
        root.getChildren().add(warning);
        // play 40s of animation
        timeline.play();
        
        
        
        
        // Button CTC
        Button submit = new Button("Login");
        submit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                String id = ((TextField)login_box.getChildren().get(1)).getText();
                String password = ((TextField)psw.getChildren().get(1)).getText();
                
                //CTC c2 = new CTC();
                //c2.start(stage);
                
                if(id.equals("theta") && password.equals("123")){
                    CTC c = new CTC();
                    c.start(stage);
                }else{
                    warning.setText("Invalid ID or Password");
                }
            }
        });
        submit.setLayoutX(scene.getWidth()*0.53);
        submit.setLayoutY(scene.getHeight()*0.55);
        root.getChildren().add(submit);
        primaryStage.show();
}
    boolean callCTC(Stage primaryStage){
        
        CTC c = new CTC();
        c.start(primaryStage);
        return false;
    }
    
    Node color(Scene scene){
        Rectangle colors = new Rectangle(scene.getWidth(), scene.getHeight(),
        new LinearGradient(0f, 1f, 1f, 0f, true, CycleMethod.NO_CYCLE, new 
         Stop[]{
            new Stop(0, Color.web("#f8bd55")),
            new Stop(0.14, Color.web("#c0fe56")),
            new Stop(0.28, Color.web("#5dfbc1")),
            new Stop(0.43, Color.web("#64c2f8")),
            new Stop(0.57, Color.web("#be4af7")),
            new Stop(0.71, Color.web("#ed5fc2")),
            new Stop(0.85, Color.web("#ef504c")),
            new Stop(1, Color.web("#f2660f")),}));

        return colors;
    }
    
    Node text(String text){
        
        Label label1 = new Label(text);
        label1.setTextFill(Color.rgb(255, 255, 255)); 
        TextField textField = new TextField ();
        HBox hb = new HBox();
        hb.getChildren().addAll(label1, textField);
        hb.setSpacing(20);
        hb.setLayoutX(scene.getWidth()*0.4 );
        hb.setLayoutY(scene.getHeight()*0.45);
        
        
        return hb;
    }
    
    Node pswText(String text){

        Label label1 = new Label(text);
        label1.setTextFill(Color.rgb(255, 255, 255)); 
        PasswordField textField = new PasswordField ();
        HBox hb = new HBox();
        hb.getChildren().addAll(label1, textField);
        hb.setSpacing(20);
        hb.setLayoutX(scene.getWidth()*0.4 );
        hb.setLayoutY(scene.getHeight()*0.45);
        
        
        return hb;
    }
    
   
    
}
    
   

//*/